package com.itheima.home_work;

import com.itheima.home_work.math_topic.*;
import com.itheima.home_work.users.Student;
import com.itheima.home_work.users.Teacher;
import com.itheima.home_work.users.User;
import com.itheima.home_work.users.UserManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class MainApp {
    // 当前登录用户
    User loginUser;

    // 用户组
    UserManager userManager = new UserManager();

    // 题库
    QuestionBank questionBank = new QuestionBank();

    public MainApp() {
    }


    /**
     * 初始化用户组
     */
    public void UsersInit() {
        // 添加两个用户
        // 添加个老师
        userManager.addUser(new Teacher("太乙真人", "123"));
        // 添加学生
        userManager.addUser(new Student("哪吒", "123"));
    }

    /**
     * 初始化题目
     */
    public void QuestionsInit() {
        // 先添加一些题目
        questionBank.addQuestionNoTip(new AddTest(4, 3));
        questionBank.addQuestionNoTip(new MultiplyTest(2, 3));
        questionBank.addQuestionNoTip(new AddTest(7, 3));
        questionBank.addQuestionNoTip(new DivideTest(9, 3));
        questionBank.addQuestionNoTip(new SubtractTest(7, 3));
        questionBank.addQuestionNoTip(new MultiplyTest(2, 8));
        questionBank.addQuestionNoTip(new AddTest(13, 3));
        questionBank.addQuestionNoTip(new AddTest(2, 65));
        questionBank.addQuestionNoTip(new DivideTest(22, 2));
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    public boolean userLogin(String username, String password) {
        this.loginUser = userManager.userLogin(username, password);
        return this.loginUser != null;
    }

    public void userTime() {
        if (this.loginUser != null) {
            if (this.loginUser.getRole() == 0) {
                this.teacherTime();
            }
            if (this.loginUser.getRole() == 1) {
                this.studentTime();
            }
        }
    }

    public void studentTime() {
        while (true) {
            System.out.println("请开始您的答题：");
            this.answer(4);
            System.out.println("成绩不服，输入Y继续挑战，否则输入其它退出！");
            Scanner sc = new Scanner(System.in);
            String next = sc.next();
            if (!next.equals("Y")) {
                break;
            }
        }

    }

    public void teacherTime() {
        while (true) {
            this.questionBank.showQuestions();
            System.out.println("是否要编辑题目,需要编辑输入Y,输入其他无需修改，并退出登录！");
            Scanner sc = new Scanner(System.in);
            String next = sc.next();
            if (next.equals("Y")) {
                System.out.println("1、添加题目");
                System.out.println("2、删除题目");
                int handle = sc.nextInt();
                if (handle == 1) {
                    this.addTest();
                }
                if (handle == 2) {
                    this.removeTest();
                }
            } else {
                break;
            }
        }
    }

    /**
     * 删除题目
     */
    public void removeTest() {
        while (true) {
            questionBank.showQuestions();
            System.out.println("请选择需要删除的题目编号, 中断操作输入 0 ");
            Scanner sc = new Scanner(System.in);
            int next = sc.nextInt();
            if (next == 0) {
                break;
            }
            if (next > questionBank.getQuestionNumber() || next < 0) {
                System.out.println("输入序号有误！");
            } else {
                questionBank.removeQuestionByIndex(next);
            }

            System.out.println("是否继续删除题目，是请按Y，否则任意输入");
            String again = sc.next();
            if (!again.equals("Y")) {
                break;
            }
        }
    }

    /**
     * 添加题目
     */
    public void addTest() {
        while (true) {
            System.out.println("请选择需要添加的题型：");
            System.out.println("1:加法题");
            System.out.println("2:减法题");
            System.out.println("3:乘法题");
            System.out.println("4:除法题");

            Scanner sc = new Scanner(System.in);
            int selectType = sc.nextInt();
            MathTest addTemp = questionBank.getTestBlank(selectType - 1);
            System.out.println("请输入被" + addTemp.getTipTxt());
            int a = sc.nextInt();
            addTemp.setA(a);
            System.out.println("请输入" + addTemp.getTipTxt());
            int b = sc.nextInt();
            addTemp.setB(b);
            questionBank.addQuestion(addTemp);
            System.out.println("题目：" + addTemp.getQuestion() + " 添加成功！");

            System.out.println("是否继续添加题目，是请按Y，否则任意输入");
            String next = sc.next();
            if (!next.equals("Y")) {
                break;
            }
        }
    }

    /**
     * 回答一轮问题
     */
    public void answer(int questionNumber) {
        System.out.println("您好，一共" + questionNumber + "道题目，请开始作答，答对一题得1分，答错不给分");
        // 打乱顺序之后回答前 questionNumber 个问题
        ArrayList<MathTest> temp = new ArrayList<>(questionBank.getMathTests());
        Collections.shuffle(temp);
        int sumScore = 0;
        for (int i = 0; i < questionNumber; i++) {
            MathTest mathTest = temp.get(i);
            int answerScore = this.answerOneQuestion(mathTest);
            sumScore += answerScore;
        }
        System.out.println("一共"+ questionNumber +"道题，每题一分，共得分" + sumScore);
        if (questionNumber == sumScore) {
            System.out.println("哇哦！算术这方面，你就是王者！佩服你");
        }
    }

    /**
     * 回答一个问题
     * @param mathTest
     */
    public int answerOneQuestion(MathTest mathTest) {
        System.out.println("请回答以下问题，机会只有一次：");
        System.out.println(mathTest.getQuestion());
        Scanner sc = new Scanner(System.in);
        int reply = sc.nextInt();
        if (questionBank.checkQuestion(mathTest, reply)) {
            System.out.println("回答正确！");
            if (loginUser instanceof Student) {
                ((Student) loginUser).setScore(((Student) loginUser).getScore());
            }
            return 1;
        } else {
            System.out.println("回答错误！");
        }
        return 0;
    }
}
